//
//  DetailViewController.m
//  星期6
//
//  Created by 宗镇荣 on 16/2/26.
//  Copyright © 2016年 zzr. All rights reserved.
//

#import "DetailViewController.h"
#import "GoodsModel.h"
#import "UIImageView+WebCache.h"
#import "CommonDefin.h"
#import "Masonry.h"
#import "CollectionDbManager.h"
#import "NearByCell.h"
#import "DetailViewController.h"
#import "DetailGoodsViewController.h"

@interface DetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>



@property (nonatomic) UICollectionView *collectionView;
@property (nonatomic) NSMutableArray *dataSource;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self getData];
}

- (void)getData {
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        CollectionDbManager *manager = [CollectionDbManager sharedCollectionDbManager];
        
        [manager getAllModel:^(NSMutableArray *modelArray) {
            
            [self.dataSource addObjectsFromArray:modelArray];
            [self.collectionView reloadData];
        }];
    });
}

- (void)configUI {
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_collectionView];
    //添加约束
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(70, 5, 0, 5));
    }];
    //cell注册
    [_collectionView registerNib:[UINib nibWithNibName:@"NearByCell" bundle:nil] forCellWithReuseIdentifier:@"collectionCell"];
}


#pragma mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    GoodsModel *model = self.dataSource[indexPath.row];
    DetailGoodsViewController *detail = [[DetailGoodsViewController alloc] init];
    detail.url = model.url;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:NO];
}
#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NearByCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    //获得model
    GoodsModel *model = self.dataSource[indexPath.item];
    [cell.appImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
    cell.appNameLabel.text = model.name;
    return cell;
}
#pragma mark UICollectionViewDelegateFlowLayout
-(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(110, 130);
}
#pragma mark 懒加载
-(NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
